package org.lzy.shop.viewmodel


import ExceptionHandler
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.response.HomeClassifyResponse
import org.lzy.shop.viewmodel.repository.ClassifyRepository
import org.lzy.shop.viewmodel.repository.lmpl.ClassifyRepositoryImpl

/**
 * 分类的VM层
 */
class ClassifyModel: BaseViewModel() {

    // 创建Repository实例
    private val repository: ClassifyRepository = ClassifyRepositoryImpl()

    // 分类数据
    val classifyLiveData = MutableLiveData<MutableList<HomeClassifyResponse>?>()



    /**
     * 获取首页分类数据
     */
    fun getHomeClassify() {
        viewModelScope.launch(Dispatchers.Main ) {
            try {
                val result = repository.getHomeClassify()
                if (result != null) {
                    classifyLiveData.value = result
                } else {
                    classifyLiveData.value = null
                }
            } catch (e: Exception) {
                val exception = ExceptionHandler.handleException(e)
                errorMessage.value = exception.errMsg ?: "获取分类数据失败"
                classifyLiveData.value = null
            }
        }
    }

}